#include "sys.h"
#include "net.h"
#include "log.h"
#include "devinfo.h"

#define QOECMD_PATH "/bin/qoecmd"
#define TIANYI_IMAGE_VERSION "/etc/image_version"
//天邑
static void _get_from_dumpobj8(char *name, char *value){
    FILE *fp;
    char line[256] = {0}, *ptr = NULL;

    fp = popen("qoecmd mdm dumpobj 8", "r");
    while(fgets(line, sizeof(line), fp)){
        if(strstr(line, name)){
            line[strlen(line) - 1] = 0;
            ptr = strstr(line, "=");
            ptr += 2;
            strcpy(value, ptr);
            break;
        }
    }
    pclose(fp);

}
int is_TEWA500AG()
{
    char manufacturer[32] = {0};

    if(access(QOECMD_PATH, F_OK) != 0){
        return 0;
    }
    
    _get_from_dumpobj8("InternetGatewayDevice.DeviceInfo.Manufacturer", manufacturer);
    if(strcmp(manufacturer, "SCTY") == 0)
        return 1;
    return 0;
}
int TEWA500AG_has_internet(){
    FILE *fp;
    char line[128] = {0};
    int has_internet_flag = 0;

    fp = popen("qoecmd wan show", "r");
    while(fgets(line, sizeof(line), fp)){
        if(strstr(line, "INTERNET_R")){
            has_internet_flag = 1;
            break;
        }
    }
    pclose(fp);
    return has_internet_flag;
}

int TEWA500AG_is_bridge()
{
    FILE *fp;
    char line[128] = {0};
    int is_bridge = 0;

    fp = popen("qoecmd wan show", "r");
    while(fgets(line, sizeof(line), fp)){
        if(strstr(line, "INTERNET_B")){
            is_bridge = 1;
            break;
        }
    }
    pclose(fp);
    return is_bridge;
}

int TEWA500AG_get_internet_r_pwd(char *usr, char *pwd){
    FILE *fp = NULL;
    char line[512] = {0};
    int obj_flg = 0, internet_flg = 0;
    char *delim = "=", *ptr, *saveptr = NULL;
    int ret = 0;

    fp = popen("qoecmd mdm dumpobj 165", "r");
    while(fgets(line, sizeof(line), fp)!= NULL){
        if(strstr(line, "dumping instance obj")){
            obj_flg = 0;
            continue;
        }
        if(strstr(line,"SubnetMask")){//SubnetMask 是一个对象开始的第一个属性
            obj_flg = 1;
            continue;
        }
        if(obj_flg){
            if(strstr(line, "INTERNET_R")){
                internet_flg = 1;
            }
        }
        if(obj_flg && internet_flg){
            line[strlen(line) - 1] = '\0';
            if(strstr(line, "Username")){
                ptr = strtok_r(line, delim, &saveptr);
                ptr = strtok_r(NULL, delim, &saveptr);
                strcpy(usr, ++ptr);
            }
            if(strstr(line, "Password")){
                ptr = strtok_r(line, delim, &saveptr);
                ptr = strtok_r(NULL, delim, &saveptr);
                strcpy(pwd, ++ptr);
            }
            if(strlen(usr) > 0 && strlen(pwd) > 0){
                ret = 1;
                break;
            }
        }

    }
    pclose(fp);
    return ret;
}

void TEWA500AG_get_mac(char* mac)
{
    getifaddr("br0", mac, "mac");
}

static void _get_loid_info(DevInfo_t *devinfo){
    FILE *fp = NULL;
    char line[128] = {0};
    char *delim = "=", *ptr, *saveptr;

    fp = popen("qoecmd mdm getpv InternetGatewayDevice.X_CT-COM_UserInfo.UserName", "r");
    fgets(line, sizeof(line), fp);
    line[strlen(line) - 1] = 0;
    ptr = strtok_r(line, delim, &saveptr);
    ptr = strtok_r(NULL, delim, &saveptr);
    if(ptr){
        strcpy(devinfo->loid, ptr);
    }
    pclose(fp);

    fp = popen("qoecmd mdm getpv InternetGatewayDevice.X_CT-COM_UserInfo.UserId", "r");
    fgets(line, sizeof(line), fp);
    line[strlen(line) - 1] = 0;
    ptr = strtok_r(line, delim, &saveptr);
    ptr = strtok_r(NULL, delim, &saveptr);
    if(ptr){
        strcpy(devinfo->loidKey, ptr);
    }
    pclose(fp);
}
void _add_iptables_rule(){
    system("iptables -D INPUT -s 222.221.16.0/24 -p udp -j ACCEPT");
    system("iptables -I INPUT -s 222.221.16.0/24 -p udp -j ACCEPT");
}

static void _get_SN(char *SN){
    FILE *fp;
    char line[265] = {0}, *ptr;
    char oui[32] = {0}, serialnumber[64] = {0};

    fp = popen("qoecmd factorycfg get", "r");
    fgets(line, sizeof(line), fp);
    line[strlen(line) - 1] = 0;
    ptr = strstr(line, "manufacturerOUI");
    if(ptr){
        ptr = strstr(ptr, ":");
        strcpy(oui, ++ptr);
    }
    pclose(fp);

    _get_from_dumpobj8("InternetGatewayDevice.DeviceInfo.SerialNumber", serialnumber);

    sprintf(SN,"%s-%s", oui, serialnumber);

}
void TEWA500AG_get_devinfo(DevInfo_t* devinfo)
{
    char buffer[128] = { 0 }, SN[32] = { 0 }, OUI[32] = { 0 };
    strcpy(devinfo->factory, "SCTY");

    _get_from_dumpobj8("InternetGatewayDevice.DeviceInfo.ProductClass", devinfo->model);

    _get_from_dumpobj8("InternetGatewayDevice.DeviceInfo.SoftwareVersion", devinfo->sw_version);

    _get_SN(devinfo->gatewaySN);

    _get_loid_info(devinfo);

    get_os_version(devinfo->osVersion);

    strcpy(devinfo->osType, "Linux");
    strcpy(devinfo->dev_type, "ONT");
    devinfo->clientPattern = TEWA500AG_is_bridge() ? 2 : 1; // 1:router 2:bridge
    devinfo->flashSize = 0;

    _add_iptables_rule();//for heart;
}